﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace themodelofline
{
    // 定义Getdata类 获取数据并进行计算
    public class Getdata
    {
        List<Data> datas = new List<Data>()
             {
             new Data
             {
                 year = 2006,
                 gdp = 2752131773358
             },
             new Data
             {
                 year = 2007,
                 gdp = 3550342737009,
             },
             new Data
             {
                 year = 2008,
                 gdp = 4594307032667,
             },
             new Data
             {
                 year = 2009,
                 gdp = 5101703073088,
             },
             new Data
             {
                 year = 2010,
                 gdp = 6087163874510,
             },
             new Data
             {
                 year = 2011,
                 gdp =7551500124197,
             },
             new Data
             {
                 year = 2012,
                 gdp = 8532229986993,
             },
             new Data
             {
                 year = 2013,
                 gdp =  9570406235659,
             },
             new Data
             {
                 year = 2014,
                 gdp =  10475682920597,
             },
             new Data
             {
                 year = 2015,
                 gdp =  11061553079871,
             },
             new Data
             {
                 year = 2016,
                 gdp = 11233276536744,
             },
             new Data
             {
                 year = 2017,
                 gdp = 12310409370894,
             },
             new Data
             {
                 year = 2018,
                 gdp = 13894817549380,
             },
             new Data
             {
                 year = 2019,
                 gdp =14279937500608,
             },
             new Data
             {
                 year = 2020,
                 gdp = 14687673892882,
             }

        };
        double ayear = 0;
        double syear = 0;
        double sgdp = 0;
        double agdp = 0;
        // 求平均年
        public double getayear()
        {

            int times = 0;
            foreach(var item in datas)
            {
                syear += item.year;
                times++;
            }
            ayear = syear / times;
            return ayear;
        }
        //求平均gdp
        public double getagdp()
        {
            int times = 0;
            foreach (var item in datas)
            {
                sgdp += item.gdp;
                times++;
            }
            agdp = sgdp / times;
            return agdp;
        }
        //求b
        public double getb( double ax, double ay)
        {
            double b = 0;
            double up = 0;
            double down = 0;

            foreach (var item in datas)
            {
                up+= (item.year-ayear)*(item.gdp-agdp);
                down += (item.year - ayear) * (item.year - ayear);
            }
                b = up / down;
            return b;
        }
        // 求a
        public double geta(double average1, double average2, double b)
        {
            double a = average2 - average1 * b;
            return a;
        }


    }
}
